/*
 * RoleTag.java
 *
 * Copyright 2012 the original author or authors(ninglong).
 *
 * Licensed under the org.geek.role.web, Version 1.0 (the "License");
 *
 */
package org.geek.role.web;

import java.util.List;

import org.geek.common.entity.ColumnModel;
import org.geek.common.tag.GridPanelTag;

/**
 * <p></p>
 * @author ninglong bhd2973@qq.com
 * @version V1.0
 */
public class RoleTag extends GridPanelTag {

	private static final long serialVersionUID = 7502388134375134920L;
	
	private String module;
	private String gridTitle;
	private String gridIconCls;
	private String gridUrl;
	private List<ColumnModel> columnModel;
	private String addUrl;
	private String delUrl;
	private String modUrl;
	
	public String getModule() {
		return module;
	}
	public void setModule(String module) {
		super.setModule(module);
		this.module = module;
	}
	public String getGridTitle() {
		return gridTitle;
	}
	public void setGridTitle(String gridTitle) {
		super.setGridTitle(gridTitle);
		this.gridTitle = gridTitle;
	}
	public String getGridIconCls() {
		return gridIconCls;
	}
	public void setGridIconCls(String gridIconCls) {
		super.setGridIconCls(gridIconCls);
		this.gridIconCls = gridIconCls;
	}
	public String getGridUrl() {
		return gridUrl;
	}
	public void setGridUrl(String gridUrl) {
		super.setGridUrl(gridUrl);
		this.gridUrl = gridUrl;
	}
	public List<ColumnModel> getColumnModel() {
		return columnModel;
	}
	public void setColumnModel(List<ColumnModel> columnModel) {
		super.setColumnModel(columnModel);
		this.columnModel = columnModel;
	}
	@Override
	public void genernateWindow(StringBuffer buffer){
		buffer.append("	var win_"+this.getId(module)+" = new Ext.Window({															").append("\r\n");
		buffer.append("			resizable : false,																					").append("\r\n");
		buffer.append("			animateTarget : document.body,																		").append("\r\n");
		buffer.append("			closeAction: 'hide',																				").append("\r\n");
		buffer.append("			plain: true,																						").append("\r\n");
		buffer.append("			modal: true,																						").append("\r\n");
		buffer.append("			items : [{																							").append("\r\n");
		buffer.append("				xtype : 'form',																					").append("\r\n");
		buffer.append("				id : 'form_"+this.getId(module)+"',																").append("\r\n");
		buffer.append("				border : false,																					").append("\r\n");
		buffer.append("				defaults : {																					").append("\r\n");
		buffer.append("					width : 260																					").append("\r\n");
		buffer.append("				},																								").append("\r\n");
		buffer.append("				bodyStyle : 'padding:15 15 15 15',																").append("\r\n");
		buffer.append("				defaultType : 'textfield',																		").append("\r\n");
		buffer.append("				labelWidth : 80,																				").append("\r\n");
		buffer.append("				labelSeparator : '：',																			").append("\r\n");
		buffer.append("				items : [{																						").append("\r\n");
		buffer.append("					id : '"+getId("roleId")+"',																	").append("\r\n");
		buffer.append("					hidden : true,																				").append("\r\n");
		buffer.append("					value : 0,																					").append("\r\n");
		buffer.append("					name : 'id'																					").append("\r\n");
		buffer.append("				},{																								").append("\r\n");
		buffer.append("					fieldLabel : '角色名',																		").append("\r\n");
		buffer.append("					allowBlank : false,																			").append("\r\n");
		buffer.append("					maxLength : 30,																				").append("\r\n");
		buffer.append("					maxLengthText : '角色名的最大长度为30个字符',														").append("\r\n");
		buffer.append("					blankText : '角色名不能为空,请输入!',																").append("\r\n");
		buffer.append("					id : '"+getId("roleName")+"',																").append("\r\n");
		buffer.append("					name : 'roleName'																			").append("\r\n");
		buffer.append("				},{																								").append("\r\n");
		buffer.append("					fieldLabel : '角色描述',																		").append("\r\n");
		buffer.append("					maxLength : 50,																				").append("\r\n");
		buffer.append("					maxLengthText : '角色描述的最大长度为50个字符',														").append("\r\n");
		buffer.append("					name : 'roleDesc'																			").append("\r\n");
		buffer.append("				}]																								").append("\r\n");
		buffer.append("			}],																									").append("\r\n");
		buffer.append("			buttons : [{																						").append("\r\n");
		buffer.append("				text : '保存',																					").append("\r\n");
		buffer.append("				iconCls : 'save',																				").append("\r\n");
		buffer.append("				handler : save_"+getId(module)+"																").append("\r\n");
		buffer.append("			},{																									").append("\r\n");
		buffer.append("				text : '重置',																					").append("\r\n");
		buffer.append("				iconCls : 'refresh',																				").append("\r\n");
		buffer.append("				handler : function() {																			").append("\r\n");
		buffer.append("  				Ext.getCmp('form_"+this.getId(module)+"').getForm().reset();								").append("\r\n");
		buffer.append("				}																								").append("\r\n");
		buffer.append("			}]																									").append("\r\n");
		buffer.append("		});																										").append("\r\n");
		
		buffer.append("  win_"+this.getId(module)+".on('show',function(){															").append("\r\n");
		buffer.append("  	Ext.getCmp('"+getId("roleName")+"').focus();															").append("\r\n");
		buffer.append("  });																										").append("\r\n");
		
		save(buffer);
	}
	
	private void save(StringBuffer buffer){
		buffer.append(" function save_"+getId(module)+"(){																			").append("\r\n");
		buffer.append("  	var form = Ext.getCmp('form_"+this.getId(module)+"').getForm();											").append("\r\n");											
		buffer.append("		var roleId = Ext.getCmp('"+getId("roleId")+"').getValue();												").append("\r\n");
		buffer.append("		var url = roleId==0?'"+this.addUrl+"':'"+this.modUrl+"';												").append("\r\n");
		buffer.append("		if (form.isValid()) {																					").append("\r\n");
		buffer.append("			form.submit({																						").append("\r\n");
		buffer.append("				url : url,																						").append("\r\n");
		buffer.append("				waitTitle : '提示',																				").append("\r\n");
		buffer.append("				method : 'POST',																				").append("\r\n");
		buffer.append("				waitMsg : '正在提交数据,请稍候.....',																	").append("\r\n");
		buffer.append("				success : function(form, action) {																").append("\r\n");
		buffer.append("					Ext.Msg.alert('提示',action.result.msg, function() {											").append("\r\n");
		buffer.append("						"+this.getId(module)+"_store.reload();													").append("\r\n");
		buffer.append("						win_"+this.getId(module)+".hide();														").append("\r\n");
		buffer.append("					});																							").append("\r\n");
		buffer.append("				},																								").append("\r\n");
		buffer.append("				failure : function(form, action) {																").append("\r\n");
		buffer.append("					Ext.Msg.alert('提示',action.result.msg, function() {											").append("\r\n");
		buffer.append("  					Ext.getCmp('"+getId("roleName")+"').focus();											").append("\r\n");
		buffer.append("						form.reset();																			").append("\r\n");
		buffer.append("					});																							").append("\r\n");
		buffer.append("				}																								").append("\r\n");
		buffer.append("			});																									").append("\r\n");
		buffer.append("		}																										").append("\r\n");
		buffer.append(" }																											").append("\r\n");
	}
	
	public String getDelUrl() {
		return delUrl;
	}
	public void setDelUrl(String delUrl) {
		super.setDelUrl(delUrl);
		this.delUrl = delUrl;
	}
	public String getModUrl() {
		return modUrl;
	}
	public void setModUrl(String modUrl) {
		this.modUrl = modUrl;
	}
	public String getAddUrl() {
		return addUrl;
	}
	public void setAddUrl(String addUrl) {
		this.addUrl = addUrl;
	}
}
